package com.aiyige.page.my.wallet;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.View;

import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.request.GetWalletDetailsListRequest;
import com.aiyige.model.response.WalletListResponse;
import com.aiyige.page.my.wallet.adapter.WalletDetailsListAdapter;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.widget.CommonDataView;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;

/**
 * 钱包明细
 */
@Route(path = ARouterConfig.WalletDetailsListPage, extras = ARouterConfig.EXTRA_NEED_LOGIN)
public class WalletDetailsListPage extends BaseActivity implements CommonDataView.RetrofitStub {


    @BindView(R.id.cdv)
    CommonDataView cdv;

    WalletDetailsListAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_wallet_details_list);
        ButterKnife.bind(this);
        initTitleLayout(getString(R.string.the_wallet_details));
        adapter = new WalletDetailsListAdapter();
        cdv.config(adapter, this);
        cdv.addItemDecoration(1, R.color.zwcolor_gray_f4f8f9);
        cdv.setRecyclerViewBgColor(ContextCompat.getColor(this, R.color.zwcolor_gray_f4f8f9));
        cdv.requestData();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                ARouter.getInstance().build(ARouterConfig.WalletSingleDetailsPage).withString("id",WalletDetailsListPage.this.adapter.getData().get(position).getId()).navigation();
            }
        });
    }

    @Override
    public Call<ResponseBody> call(long currentPage) {
        return GetWalletDetailsListRequest.newBuilder()
                .pageNum(currentPage + "")
                .build();
    }

    @Override
    public CommonDataView.HandleResult handleResponse(String respJson, List resultList) {
        try {
            WalletListResponse response = JsonUtil.toObject(respJson, WalletListResponse.class);
            if (response != null && !ListUtil.isEmpty(response.getContent())) {
                resultList.addAll(response.getContent());
                return new CommonDataView.HandleResult(response.isLast());
            }
            return new CommonDataView.HandleResult(true);
        } catch (Exception e) {
            return new CommonDataView.HandleResult(e.getMessage());
        }
    }


}
